//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.anolesoft.twelveFactory.planManagement.controller;

import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.system.fac.entity.Employee;
import com.anolesoft.system.fac.service.IEmployeeService;
import com.anolesoft.twelveFactory.planManagement.entity.Outsourcing;
import com.anolesoft.twelveFactory.planManagement.service.OutsourcingService;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.Constants;
import com.anolesoft.yr.util.HttpStatus;
import com.anolesoft.yr.util.PageData;
import com.anolesoft.yr.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.time.LocalDate;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping({"/twelveFactory/outsourcing"})
public class OutsourcingController {
    @Autowired
    private OutsourcingService outsourcingService;
    @Autowired
    private IEmployeeService iEmployeeService;

    public OutsourcingController() {
    }
    //外协 增删改查
    @PostMapping(
            value = {"/listOutsourcing"},
            produces = {"application/json"}
    )
    public AjaxResult selectOutsourcing(@RequestBody Outsourcing outsourcing) {
        PageData<List<Outsourcing>> pageData = this.outsourcingService.listOutsourcing(outsourcing);
        PageResult pageResult = new PageResult((List)pageData.getItems(), outsourcing.getPageSize(), outsourcing.getPageNo(), pageData.getTotal());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageResult);
    }

    @PostMapping({"/insert"})
    public AjaxResult insert(@RequestBody Outsourcing outsourcing) {
        outsourcing.setCjsj(Date.valueOf(LocalDate.now()));
        outsourcing.setCjr(SecurityUtils.getLoginUser().getUsername());
        Employee employee = (Employee)this.iEmployeeService.getById(SecurityUtils.getLoginUser().getEmpUid());
        if(employee!=null) {
            if (employee.getFacCode() != null) {
                outsourcing.setCdh(employee.getFacCode());
            }
        }
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, this.outsourcingService.save(outsourcing));
    }

    @PostMapping({"/update"})
    public AjaxResult update(@RequestBody Outsourcing outsourcing) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.MODIFY_SUCCESS_MESSAGE, this.outsourcingService.updateById(outsourcing));
    }

    @GetMapping({"/delete"})
    public AjaxResult delete(String objuid) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, this.outsourcingService.removeById(objuid));
    }

    @GetMapping({"/selectById"})
    public AjaxResult selectById(String objuid) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, this.outsourcingService.selectById(objuid));
    }
}
